import API from '@api'
import {TAB, CONTENT} from '@state/mutations-types/outreach-group'
export const state = {
  groupList: [],
  addPosition: [],
  delPosition: {},
  editPosition: {},
  movePosition: {},
  isLastDepartment: false
}

export const getters = {}

export const mutations = {
  [TAB.CHANGE_TAB_STATUS] (state, args) {
    const {current} = args
    closeAll(state.groupList)
    current.isChecked = !current.isChecked
  },
  [TAB.ADD_DEPARTMENT] (state, department) {
    state.addPosition.unshift(department)
  },
  [TAB.SET_ADD_POSITION] (state, addPosition) {
    state.addPosition = addPosition.position
    state.isLastDepartment = addPosition.isLastDepartment
  },
  [TAB.SET_DEL_POSITION] (state, delPosition) {
    state.delPosition = delPosition
  },
  [TAB.SET_EDIT_POSITION](state, editPosition) {
    state.editPosition = editPosition.current
  },
  [TAB.SET_MOVE_POSITION] (state, movePosition) {
    state.movePosition = movePosition
  },
  [TAB.DELETE_DEPARTMENT] (state) {
    const {parent, index} = state.delPosition
    parent.splice(index, 1)
  },
  [TAB.EDIT_DEPARTMENT] (state, name) {
    state.editPosition.name = name
  },
  [TAB.MOVE_DEPARTMENT] (state, args) {
    const {moveType} = args
    const {index, current, parent} = state.movePosition
    if (moveType === 'up' && index === 0) {
      return
    }
    if (moveType === 'down' && index === parent.length -1) {
      return
    }
    if (moveType === 'up' && index !== 0) {
      parent.splice(index, 1)
      parent.splice(index -1 , 0, current)
    } else if (moveType === 'down' && index !== parent.length -1 ){
      parent.splice(index, 1)
      parent.splice(index + 1, 0, current)
    }
    closeAll(state.groupList)
  }
}

export const actions = {
  reqGroupList({commit, state, dispacth}, args) {
    // API.OutreachGroup().then(res => {
    //
    // })
  },
  addDepartment({commit, state, rootState}, args) {
    return API.OutreachGroup.addDepartment().then(() => {
      const department = {
        name: rootState.oGModal.name ,
        list: [],
        isChecked: false
      }
      commit(TAB.ADD_DEPARTMENT, department)
    })
  },
  deleteDepartment({commit, state}, args) {
    return API.OutreachGroup.addDepartment().then(() => {
      commit(TAB.DELETE_DEPARTMENT)
    })
  },
  editDepartment({commit, state, rootState}, args) {
    return API.OutreachGroup.addDepartment().then(() => {
      commit(TAB.EDIT_DEPARTMENT, rootState.oGModal.name)
    })
  },
  moveDepartment({commit, state}, args) {
    return API.OutreachGroup.addDepartment().then(() => {
      commit(TAB.MOVE_DEPARTMENT, args)
    })
  },
  changeTab({commit,state, dispatch, rootState}, args) {
    // return API.OutreachGroup.addDepartment().then(() => {
    //     //
    //     // })
    if (state.isLastDepartment) {
      dispatch(`oGContent/reqStaffList`, {id: 0}, {root: true})
    } else {
      commit(`oGContent/${CONTENT.CLEAR_STAFF_LIST}`, {}, {root: true})
    }
    commit(TAB.CHANGE_TAB_STATUS, args)
  }
}

function closeAll(arr) {
  arr.forEach(item => {
    item.isChecked = false
    if (item.list) {
      return closeAll(item.list)
    }
  })
}
